package cn.jbolt.ec.wx;

import cn.jbolt.common.model.BecApp;
import cn.jbolt.common.model.BecSysconfig;
import com.jfinal.kit.PropKit;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;


import javax.print.attribute.IntegerSyntax;
import java.util.HashMap;
import java.util.Map;


public class GenService {

    private final static BecApp staticBecApp;

    private static Map<String,WxMpService> mapWxMpService;

    public  static WxMpService getWxMpService(String name){

        return mapWxMpService.get(name);
    }
    static {
        /**
         * 1 共享商城
         * 2 信息中心
         * 3 商城
         */


        mapWxMpService = new HashMap<String, WxMpService>();
        // 在系统配置里拿出激活哪个公众号，以利于平行部署。
        BecSysconfig config = new BecSysconfig().dao().findById(1);
        staticBecApp = appendWxMpService(Integer.parseInt(config.getValue()),"jmd");

        
    }
    public static BecApp getApp(){
        return GenService.staticBecApp;
    }


    static BecApp appendWxMpService(int appid, String name){
        BecApp becApp=new BecApp().dao().findById(appid);
        WxMpService wxService = new WxMpServiceImpl();

        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(becApp.getAppid()); // 设置微信公众号的appid
        config.setSecret(becApp.getAppSecret()); // 设置微信公众号的appSecret
        config.setToken(becApp.getOtherCfgStr1()); // 设置微信公众号的token
        config.setAesKey(becApp.getOtherCfgStr3()); // 设置微信公众号的EncodingAESKey

        wxService.setWxMpConfigStorage(config);
        mapWxMpService.put(name,wxService);
        return becApp;


    }



}
